package com.cdtu.labpreview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.labpreview.common.Result;
import com.cdtu.labpreview.controller.vo.FeelingVo;
import com.cdtu.labpreview.controller.vo.FeelingVoByBaseId;
import com.cdtu.labpreview.entity.*;
import com.cdtu.labpreview.entity.Class;
import com.cdtu.labpreview.mapper.*;
import com.cdtu.labpreview.service.IFeelingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FeelingServiceImpl extends ServiceImpl<FeelingMapper, Feeling> implements IFeelingService {

    @Autowired
    private BaseIntroductionMapper baseIntroductionMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private FeelingMapper feelingMapper;
    @Autowired
    private FeelingVoByBaseIdMapper feelingVoByBaseIdMapper;

    @Override
    public Result getFeelingList(int no,int type) {
        if(type==2){
            LambdaQueryWrapper<BaseIntroduction> lqw=new LambdaQueryWrapper<>();
            lqw.eq(BaseIntroduction::getTno,no);
            List<BaseIntroduction> baseIntroductions = baseIntroductionMapper.selectList(lqw);

            List<FeelingVo> feelingVoList = baseIntroductions.stream().map(baseIntroduction -> {
                FeelingVo feelingVo = new FeelingVo();
                long baseId = baseIntroduction.getId();
                String labName = baseIntroduction.getName();
                long classId = baseIntroduction.getClassId();
                Class aClass = classMapper.selectById(classId);
                String className = aClass.getName();

                //插入心得体会首页显示（实验名及班级）
                feelingVo.setBaseId(baseId);
                feelingVo.setLabName(labName);
                feelingVo.setClassName(className);
                //插入该实验名该班级下所有同学提交的心得体会
                LambdaQueryWrapper<Feeling> lqw2=new LambdaQueryWrapper<>();
                lqw2.eq(Feeling::getBaseId,baseId);
                List<Feeling> feelings = feelingMapper.selectList(lqw2);
                List<FeelingVoByBaseId> feelingList = feelings.stream().map(feeling -> {
                    int sno = feeling.getSno();
                    LambdaQueryWrapper<Student> lqw3 = new LambdaQueryWrapper<>();
                    lqw3.eq(Student::getSno, sno);
                    Student student = studentMapper.selectOne(lqw3);
                    String studentName = student.getName();
                    String content = feeling.getContent();

                    FeelingVoByBaseId feelingVoByBaseId = new FeelingVoByBaseId();
                    feelingVoByBaseId.setContent(content);
                    feelingVoByBaseId.setStudentName(studentName);
                    return feelingVoByBaseId;
                }).collect(Collectors.toList());

                feelingVo.setFeelingList(feelingList);

                return feelingVo;
            }).collect(Collectors.toList());
            return Result.ok(feelingVoList);
        }else {
            LambdaQueryWrapper<Student> lqw=new LambdaQueryWrapper<>();
            lqw.eq(Student::getSno,no);
            Student student = studentMapper.selectOne(lqw);

            long classId = student.getClassId();
            LambdaQueryWrapper<BaseIntroduction> lqw2=new LambdaQueryWrapper<>();
            lqw2.eq(BaseIntroduction::getClassId,classId);
            List<BaseIntroduction> baseIntroductions = baseIntroductionMapper.selectList(lqw2);
            List<FeelingVo> feelingVoList = baseIntroductions.stream().map(baseIntroduction -> {
                FeelingVo feelingVo = new FeelingVo();
                long baseId = baseIntroduction.getId();
                String labName = baseIntroduction.getName();
                feelingVo.setBaseId(baseId);
                feelingVo.setLabName(labName);

                LambdaQueryWrapper<Feeling> lqw3=new LambdaQueryWrapper<>();
                lqw3.eq(Feeling::getBaseId,baseId);
                lqw3.eq(Feeling::getSno,no);
                List<Feeling> feelingList = feelingMapper.selectList(lqw3);
                List<FeelingVoByBaseId> feelingEndList = feelingList.stream().map(feeling -> {
                    FeelingVoByBaseId f = new FeelingVoByBaseId();
                    BeanUtils.copyProperties(feeling, f);
                    return f;
                }).collect(Collectors.toList());
                feelingVo.setFeelingList(feelingEndList);
                return feelingVo;
            }).collect(Collectors.toList());
            return Result.ok(feelingVoList);
        }
    }


    @Override
    public Result addFeeling(Feeling feeling,int sno) {
        feeling.setSno(sno);
        feelingMapper.insert(feeling);
//        LambdaQueryWrapper<Student> lqw=new LambdaQueryWrapper<>();
//        lqw.eq(Student::getSno,sno);
//        Student student = studentMapper.selectOne(lqw);
//        LambdaQueryWrapper<BaseIntroduction> lqw2=new LambdaQueryWrapper<>();
//        lqw2.eq(BaseIntroduction::getClassId,student.getClassId());
//        List<BaseIntroduction> baseIntroductionList = baseIntroductionMapper.selectList(lqw2);

//        BaseFeeling baseFeeling=new BaseFeeling();
//        baseFeeling.setFeelingId(feeling.getId());
//        baseFeeling.setBaseId(feeling.getBaseId());
//        baseFeelingMapper.insert(baseFeeling);
        return Result.ok("提交成功");
    }

    @Override
    public Result updateFeeling(Feeling feeling) {
        Feeling f = feelingMapper.selectById(feeling.getId());
        f.setContent(feeling.getContent());
        feelingMapper.updateById(f);
        return Result.ok("修改成功！");
    }

    @Override
    public Result deleteFeeling(long id) {
        feelingMapper.deleteById(id);
        return Result.ok("删除成功！");
    }

    @Override
    public Result getFeelingById(long id) {
        LambdaQueryWrapper<Feeling> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Feeling::getBaseId,id);
        List<Feeling> feelings = feelingMapper.selectList(lqw);
        List<FeelingVoByBaseId> collect = feelings.stream().map(feeling -> {
            String content = feeling.getContent();
            int sno = feeling.getSno();
            LambdaQueryWrapper<Student> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(Student::getSno, sno);
            Student student = studentMapper.selectOne(lqw2);
            String studentName = student.getName();

            FeelingVoByBaseId feelingVoByBaseId = new FeelingVoByBaseId();
            feelingVoByBaseId.setContent(content);
            feelingVoByBaseId.setStudentName(studentName);
            return feelingVoByBaseId;
        }).collect(Collectors.toList());
        return Result.ok(collect);
    }
}
